#include "bbssparkstore.h"

#include <qrcutil.h>
#include <response.h>

BBsSparkStore::BBsSparkStore() {}

QString BBsSparkStore::name()
{
    return "星火应用商店论坛";
}

QString BBsSparkStore::prefix()
{
    return "";
}

QStringList BBsSparkStore::searching(const QString &text)
{
    return QStringList();
}

QString BBsSparkStore::content(const QString &text)
{

    return QString();
}

bool BBsSparkStore::autoLoad()
{
    return true;
}

/**
 * @brief BBsSparkStore::searching_item
 * @param text
 * for i in 20 40 60 80 100; do curl -s "https://bbs.spark-app.store/api/discussions?include=user%2ClastPostedUser%2Ctags%2Ctags.parent%2CfirstPost%2CfirstPost&sort=-createdAt&page%5Boffset%5D=$i"; done | jq .data | jq -s | jq .[] | jq -s add | code -
 * @return
 */

QList<QListWidgetItem *> BBsSparkStore::searching_item(const QString &text)
{
    QList<QListWidgetItem *> list;
    auto items = Response(0, QR("://spark-store-bbs.json")).toList();

    foreach (auto data, items) {
        auto var = data.toMap();
        auto attributes = var.value("attributes").toMap();
        auto title = attributes.value("title").toString();
        auto shareUrl = attributes.value("shareUrl").toString();
        auto createdAt = attributes.value("createdAt").toString();

        auto dt = QDateTime::fromString(createdAt, Qt::ISODateWithMs);
        qDebug() << dt;

        QListWidgetItem *item = newItem();
        item->setText(dt.toString("yyyy年MM月dd日") + ": " + title);
        item->setData(990, shareUrl);

        list << item;
    }

    return list;
}

QString BBsSparkStore::content_item(const QListWidgetItem *item)
{
    auto shareUrl = item->data(990).toString();
    return shareUrl.replace("\\","");
}

Wiki::SearchMode BBsSparkStore::searchMode()
{
    return ListItem;
}

Wiki::ContentMode BBsSparkStore::contentMode()
{
    return Url;
}
